package com.mindbook.vip.gawxchatinfo.controller.question;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindbook.vip.gawxchatinfo.entity.question.OftenAskQuestion;
import com.mindbook.vip.gawxchatinfo.exception.crud.CurdException;
import com.mindbook.vip.gawxchatinfo.service.question.OftenAskQuestionService;
import com.mindbook.vip.gawxchatinfo.utils.crud.CrudUtils;
import com.mindbook.vip.gawxchatinfo.utils.result.Result;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultEnum;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @NAME: wxb
 * @DATE: 2022/6/24
 * @DESCRIPTION:
 **/
@Api(value = "常问问题")
@RestController
@RequestMapping("oftenAskQuestion")
public class OftenAskQuestionController {

    @Autowired private OftenAskQuestionService oftenAskQuestionService;

    @PostMapping("/")
    @ApiOperation(value = "新增")
    public Result add(OftenAskQuestion oftenAskQuestion){

        Integer add = oftenAskQuestionService.add(oftenAskQuestion);

        CrudUtils.validateAddResult(add);

        return ResultUtils.genSuccessResult(ResultEnum.DATA_ADD_SUCCESS.getMsg());

    }

    @PutMapping("/")
    @ApiOperation(value = "修改")
    public Result put(OftenAskQuestion oftenAskQuestion){

        Integer put = oftenAskQuestionService.put(oftenAskQuestion);

        CrudUtils.validateUpdateResult(put);

        return ResultUtils.genSuccessResult(ResultEnum.DATA_UPDATE_SUCCESS.getMsg());

    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public Result del(@PathVariable Integer id){

        Integer del = oftenAskQuestionService.removeByPrimaryKey(id);

        CrudUtils.validateDeleteResult(del);

        return ResultUtils.genSuccessResult(ResultEnum.DATA_DELETE_SUCCESS.getMsg());

    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询,根据问题 模糊查询")
    public Result getListByPage(@RequestParam(value = "pageNum",defaultValue="1") Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                String questionStem){

        PageHelper.startPage(pageNum,pageSize);

        List<OftenAskQuestion> all = oftenAskQuestionService.findAll(questionStem);

        PageInfo<OftenAskQuestion> pageInfo  = new PageInfo<>(all);

        return ResultUtils.genSuccessResult(pageInfo);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询数据详情")
    public Result findDetailsById(@PathVariable Integer id){
        OftenAskQuestion byPrimaryKey = oftenAskQuestionService.findByPrimaryKey(id);

        return ResultUtils.genSuccessResult(byPrimaryKey);
    }


}
